//
//  MemberListTableCellTableViewCell.m
//  Leo01
//
//  Created by 张旭 on 15/9/24.
//  Copyright (c) 2015年 张旭. All rights reserved.
//

#import "MemberListTableCellTableViewCell.h"
#import "Tooler.h"
#import "DataManager.h"
@implementation MemberListTableCellTableViewCell
@synthesize navController;
@synthesize avatarImageView;
@synthesize nameLabel;
@synthesize managerTypeLabel;
@synthesize phoneNumberLabel;

@synthesize isManager;
@synthesize ImageMap;
@synthesize fromContact;

//-(MemberListTableCellTableViewCell *)getandSetData:(NSDictionary *)subdic{
//    if (isManager) {
//        managerTypeLabel.text = [self getManagerNameByNumber:[subdic objectForKey:@"managertype"]];
//    }
//    else {
//        managerTypeLabel.text = @"";
//    }
//    nameLabel.text = [subdic objectForKey:@"name"];
//    phoneNumberLabel.text = [subdic objectForKey:@"phonenumber"];
//    [avatarImageView setImage:[UIImage imageNamed:[self getImageName:[subdic objectForKey:@"avatar"]]]];
//    return self;
//}

-(MemberListTableCellTableViewCell *)getandSetDataWithManager:(Manager *)manager Type:(int)type{
//    phoneNumberLabel.text = manager.phone;
//    nameLabel.text = manager.alias;
//    managerTypeLabel.text = [self getManagerNameByNumber:manager.type];
//    NSString *picUrl = [[Tooler getDocumentDirectory] stringByAppendingString:manager.pic_url];
//    if (manager.pic_url==NULL||[manager.pic_url isEqualToString:@"(null)"]||[manager.pic_url isEqualToString:@""])
//    {
//        avatarImageView.image = [UIImage imageNamed:@"avater_brother"];
//    }
//    else
//    {
//        avatarImageView.image = [UIImage imageWithContentsOfFile:picUrl];
//    }
//    return self;
    
    phoneNumberLabel.text = manager.phone;
    User *user=[User sharedSingleton];
    //**type=1代表成员管理cell，2为联系人cell
    if (type==2){
        if(user!=NULL&&user.uid!=NULL&&manager!=NULL&&manager.user_id!=NULL&&[manager.user_id isEqualToString:[NSString stringWithFormat:@"%@",user.uid]]){
            nameLabel.text = [NSString stringWithFormat:@"我(%@)",manager.alias];
        }
        else{
            nameLabel.text = manager.alias;}

    }
    else{
        if(user!=NULL&&user.uid!=NULL&&manager!=NULL&&manager.user_id!=NULL&&[manager.user_id isEqualToString:[NSString stringWithFormat:@"%@",user.uid]]){
            nameLabel.text = [NSString stringWithFormat:@"我(%@)",manager.nickName];
        }
        else{
            nameLabel.text = manager.nickName;}
        
    }
    
        if (manager.type==1){
        managerTypeLabel.text = @"主管理员";
    }
    else{
        managerTypeLabel.text = @"副管理员";
    }
    if (fromContact)
    {
        nameLabel.text=manager.alias;
        //managerTypeLabel.text=@"";
    }
    
    
    NSRange range;
    NSString *photoNameString;
    range = [manager.pic_url rangeOfString:@"/headimg/"];
    if (range.location != NSNotFound) {
        NSLog(@"found at location = %d, length = %d",range.location,range.length);
        photoNameString=[manager.pic_url substringFromIndex:range.location];
        NSLog(@"%@",photoNameString);
        
    }else{
        NSLog(@"Not Found");
    }
    //默认头像
    avatarImageView.image = [UIImage imageNamed:@"avater_brother"];
    user=[User sharedSingleton];
    if(user!=NULL&&[manager.user_id isEqualToString:[NSString stringWithFormat:@"%@",user.uid]])
    {
        if(user.pic_url!=NULL&&![user.pic_url isEqualToString:@"(null)"]&&![user.pic_url isEqualToString:@""])
        {
            avatarImageView.image=[UIImage imageNamed:[[Tooler getDocumentDirectory] stringByAppendingString:user.pic_url]];
        }
        else
        {
            DataManager *dataManager=[[DataManager alloc]init];
            Manager *userManager=[dataManager getManagerByUid:user.uid];
            if(userManager.pic_url!=NULL&&![userManager.pic_url isEqualToString:@"(null)"]&&![userManager.pic_url isEqualToString:@""])
            {
                NSString *headUrl=[NSString stringWithFormat:@"%@%@",[Tooler getDocumentDirectory],userManager.pic_url];
                avatarImageView.image=[UIImage imageWithContentsOfFile:headUrl];
            }

        }
    }
//    DataManager *dataManager=[[DataManager alloc]init];
//    manager=[dataManager getManagerByUid:user.uid];
    if(manager.pic_url!=NULL&&![manager.pic_url isEqualToString:@"(null)"]&&![manager.pic_url isEqualToString:@""])
    {
        NSString *headUrl=[NSString stringWithFormat:@"%@%@",[Tooler getDocumentDirectory],photoNameString];
        avatarImageView.image=[UIImage imageWithContentsOfFile:headUrl];
    }

    managerTypeLabel.hidden=NO;
    return self;
}

-(MemberListTableCellTableViewCell *)getandSetDataWithContact:(Contact *)contact{
    phoneNumberLabel.text = contact.phoneNum;
    if(contact.contactname!=NULL&&![contact.contactname isEqualToString:@"(null)"]&&![contact.contactname isEqualToString:@""])
    {
        User *user=[User sharedSingleton];
        if(user!=NULL&&[contact.user_id isEqualToString:[NSString stringWithFormat:@"%@",user.uid]]){
            nameLabel.text = [NSString stringWithFormat:@"我(%@)",contact.contactname];
        }
        else{
            nameLabel.text = contact.contactname;}
    
    }
    else
    {
        DataManager *dataManager=[[DataManager alloc]init];
        Manager *manager=[dataManager getManager:contact.contact_id];
        if (manager!=nil)
        {
            User *user=[User sharedSingleton];
            if(user!=NULL&&[manager.user_id isEqualToString:[NSString stringWithFormat:@"%@",user.uid]]){
                nameLabel.text = [NSString stringWithFormat:@"我(%@)",manager.alias];
            }
            else{
                nameLabel.text = manager.alias;}
        }
    }
    managerTypeLabel.text = @"";
//    NSURL *url = [NSURL URLWithString:(contact.photo_url)];
//    NSLog(@"图片URL是%@",url);
//    NSData *avatarImageData = [NSData dataWithContentsOfURL:url];
//    if (avatarImageData==NULL)
//    {
//        avatarImageView.image = [UIImage imageNamed:@"avater_brother"];
//    }
//    else
//    {
//        avatarImageView.image = [UIImage imageWithData:avatarImageData];
//    }
    
    NSRange range;
    NSString *photoNameString;
    range = [contact.photo_url rangeOfString:@"/headimg/"];
    if (range.location != NSNotFound) {
        NSLog(@"found at location = %d, length = %d",range.location,range.length);
        photoNameString=[contact.photo_url substringFromIndex:range.location];
        NSLog(@"%@",photoNameString);
        
    }else{
        NSLog(@"Not Found");
    }
    
    
    if(contact.photo_url!=NULL&&![contact.photo_url isEqualToString:@"(null)"]&&![contact.photo_url isEqualToString:@""])
    {
        NSString *headUrl=[NSString stringWithFormat:@"%@%@",[Tooler getDocumentDirectory],photoNameString];
        avatarImageView.image=[UIImage imageWithContentsOfFile:headUrl];
    }
    else
    {
        //默认头像
        avatarImageView.image = [UIImage imageNamed:@"avater_brother"];
        //avatarImageView.backgroundColor=[UIColor whiteColor];

    }

    
//    if(contact.photo_url!=NULL&&![contact.photo_url isEqualToString:@"(null)"]&&![contact.photo_url isEqualToString:@""])
//    {
//        NSString *headUrl=[NSString stringWithFormat:@"%@%@",[Tooler getDocumentDirectory],contact.photo_url];
//        avatarImageView.image=[UIImage imageWithContentsOfFile:headUrl];
//    }
//    else
//    {
//        avatarImageView.image = [UIImage imageNamed:@"avater_brother"];
//    }


    return self;
}
-(MemberListTableCellTableViewCell *)getandSetDataWithWatch:(Watch *)watch{
    phoneNumberLabel.text = watch.sim;
    
    nameLabel.text=watch.nickname;
    managerTypeLabel.text = @"手表";
    //managerTypeLabel.text = @"";
    //    NSURL *url = [NSURL URLWithString:(contact.photo_url)];
    //    NSLog(@"图片URL是%@",url);
    //    NSData *avatarImageData = [NSData dataWithContentsOfURL:url];
    //    if (avatarImageData==NULL)
    //    {
    //        avatarImageView.image = [UIImage imageNamed:@"avater_brother"];
    //    }
    //    else
    //    {
    //        avatarImageView.image = [UIImage imageWithData:avatarImageData];
    //    }
    
    NSRange range;
    NSString *photoNameString;
    range = [watch.pic_url rangeOfString:@"/headimg/"];
    if (range.location != NSNotFound) {
        NSLog(@"found at location = %d, length = %d",range.location,range.length);
        photoNameString=[watch.pic_url substringFromIndex:range.location];
        NSLog(@"%@",photoNameString);
        
    }else{
        NSLog(@"Not Found");
    }
    
    
    if(watch.pic_url!=NULL&&![watch.pic_url isEqualToString:@"(null)"]&&![watch.pic_url isEqualToString:@""])
    {
        NSString *headUrl=[NSString stringWithFormat:@"%@%@",[Tooler getDocumentDirectory],photoNameString];
        avatarImageView.image=[UIImage imageWithContentsOfFile:headUrl];
    }
    else
    {
        if(watch.gender==1)//女孩
        {
            avatarImageView.image=[UIImage imageNamed:@"avater_girl.png"];
        }
        else//男孩
        {
            avatarImageView.image=[UIImage imageNamed:@"avater_boy.png"];
        }
    }

    
    return self;
}


- (NSString *)getManagerNameByNumber:(int)managertype{
    switch (managertype) {
        case 1:
            return @"主管理员";
            break;
        case 2:
            return @"副管理员";
            break;
        default:
            return nil;
            break;
    }
}

//just for test
- (NSString *)getImageName:(NSString *)name{
    switch ([ImageMap indexOfObject:name]) {
        case 0:
            return @"avater_boy";
            break;
        case 1:
            return @"avater_girl";
            break;
        default:
            return nil;
            break;
    }
}

- (void)awakeFromNib {
    // Initialization code
    //temp code
    ImageMap = [NSArray arrayWithObjects:@"boy",@"girl",@"girl",@"girl",@"girl",nil];
    self.avatarImageView.layer.cornerRadius = self.avatarImageView.frame.size.width / 2;
    self.avatarImageView.clipsToBounds = YES;
    
    nameLabel.font=[UIFont boldSystemFontOfSize:17];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
